/**
 * description: 轨迹记录
 * fileName: Routes.ts
 * author: ssr
 * date: 2022-08-17 10:33:22
 * version: V1.0.0
 */

import { ISideStep } from "@@/typings/observation";
import { CustomViewer } from "../CustomCesium/CustomCesium";
import RoutesUtils from "./RoutesUtils";
import { useSimulationStore } from "@/pinia/stores/index";
import { PathGroupId } from "@@/enums/observation";

interface Routes {
  id: string;
  positions: { [propName: string]: number[] };
}

export default class RoutesManager {
  // 所有实体轨迹
  private entityRoutes: Routes[];

  constructor() {
    this.entityRoutes = [];
  }

  /**
   * @descripttion: 轨迹是否已存在
   * @param {string} id 组id
   * @return {*}
   */
  isExists(id: string) {
    if (!id) return true;
    return this.entityRoutes.findIndex((item) => item.id === id) >= 0;
  }

  /**
   * 获取路径点
   * @param id
   * @returns
   */
  getRoutes(id: string) {
    return this.entityRoutes.find((item) => item.id === id);
  }

  /**
   * 初始化轨迹
   * @param customViewer
   * @param step
   * @param visualAngle
   */
  async createInitPath(
    customViewer: CustomViewer,
    step: ISideStep,
    visualAngle: string
  ) {
    const simulStore = useSimulationStore(),
      a = step && RoutesUtils.createInitPath(customViewer, step, visualAngle),
      group =
        a &&
        customViewer.animationManager?.createAnimationGroup(
          PathGroupId[visualAngle],
          PathGroupId[visualAngle],
          a.period,
          a.animations
        ),
      result =
        group &&
        (await customViewer.animationManager?.renderAnimationGroup(group));
    group &&
      (group.datasource.show =
        simulStore.currentPerspective === visualAngle && simulStore.isShowPath);
    // 更新路径
    this.updateRoutes(PathGroupId[visualAngle], a.newRoutes);
  }

  /**
   * 更新轨迹
   * @param customViewer
   * @param step
   * @param visualAngle
   */
  updatePath(customViewer: CustomViewer, step: ISideStep, visualAngle: string) {
    const simulStore = useSimulationStore(),
      group = customViewer.animationManager?.getAnimationGroup(
        PathGroupId[visualAngle]
      );
    group &&
      (group.datasource.show =
        simulStore.currentPerspective === visualAngle && simulStore.isShowPath);
    const positions = this.getRoutes(PathGroupId[visualAngle])?.positions;
    if (group) {
      const newRoutes = RoutesUtils.updatePath(
        customViewer,
        group,
        step,
        visualAngle,
        positions
      );
      this.updateRoutes(PathGroupId[visualAngle], newRoutes);
    }
  }

  /**
   * @descripttion: 维护轨迹信息
   * @param {*}
   * @return {*}
   */
  updateRoutes = (id: string, positions: { [propName: string]: number[] }) => {
    // 判断是否已存在
    if (!id) return;
    let flag = true;
    this.entityRoutes.forEach((item, index) => {
      if (item.id === id) {
        item.positions = Object.assign({}, positions);
        this.entityRoutes.splice(index, 1, item);
        flag = false;
        return;
      }
    });
    if (flag) {
      this.entityRoutes.push({
        id,
        positions,
      });
    }
  };

  /**
   * 清空历史轨迹数据
   */
  removeAll() {
    this.entityRoutes.forEach((item, index) => {
      item.positions = {};
      this.entityRoutes.splice(index, 1);
    });
  }
}

export function createRoutesManager() {
  return new RoutesManager();
}
